package com.hxq.library.dao.impl;

import com.hxq.library.dao.IUserDao;
import com.hxq.library.entity.User;
import com.hxq.library.utils.JavaDateConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author: Ljx
 * @Date: 2021/11/24 13:27
 * @role:
 */
public class UserDaoImpl implements IUserDao {
    @Override
    public boolean login(Connection connection, User user) throws SQLException {
        ResultSet resultSet = null;
        String sql = "select * from user where student_id = ?";
        PreparedStatement preparedStatement = null;
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, user.getStudentId());
        resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            return false;
        }else {
            String sql1 = "insert into user values(null,?,?,?,?,0,0,null )";
            PreparedStatement preparedStatement1 =null;
            preparedStatement1 = connection.prepareStatement(sql1);
            preparedStatement1.setString(1,user.getStudentId());
            preparedStatement1.setString(2,user.getStudentMail());
            preparedStatement1.setString(3,user.getStudentNumber());
            preparedStatement1.setString(4,user.getStudentPassword());
            preparedStatement1.executeUpdate();
            return true;
        }
    }

    @Override
    public boolean register(Connection connection,String account,String password) {
        ResultSet resultSet = null;
        String sql = "select * from user where student_id = ?";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,account);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                String pwd = resultSet.getString("student_password");
                if (pwd.equals(password)){
                    return true;
                }else {
                    return false;
                }
            }else {
                return false;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (preparedStatement != null) {
                        preparedStatement.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    JavaDateConnection.closeCon(connection);
                }
            }
        }
        return true;
    }

    @Override
    public boolean pay(Connection connection,int money, String account,String password,String number) throws SQLException {
        ResultSet resultSet = null;
        String sql = "select * from user where student_id = ?";
        PreparedStatement preparedStatement = null;
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, account);
        resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
            String pwd = resultSet.getString("student_password");
            String unm = resultSet.getString("student_number");
            int my = resultSet.getInt("student_money");
            if (pwd.equals(password) && unm.equals(number)) {
                String sql1 = "update user" + " set student_money = ?" + " where student_id = ?;";
                PreparedStatement preparedStatement1 = null;
                preparedStatement1 = connection.prepareStatement(sql1);
                preparedStatement1.setInt(1, my + money);
                preparedStatement1.setString(2, account);
                preparedStatement1.executeUpdate();
            } else {
                return false;
            }
        } else {
            return false;
        }
        return true;
    }

    @Override
    public int selectMoney(Connection connection, String account) throws SQLException {
        ResultSet resultSet = null;
        String sql = "select * from user where student_id = ?";
        PreparedStatement preparedStatement = null;
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,account);
        resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            int money = resultSet.getInt("student_money");
            return money;
        }
        return -1;
    }

    @Override
    public int payVip(Connection connection, String account) throws SQLException {
        int money = selectMoney(connection, account);
        ResultSet resultSet = null;
        String sql = "select * from user where student_id = ?";
        PreparedStatement preparedStatement = null;
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,account);
        resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            int i = resultSet.getInt("student_member");
            if (i == 1){
                return 2;
            }else if (money < 300){
                return 0;
            }else {
                String sql1 = "update user set student_member=? , student_money=? where student_id = ? ";
                PreparedStatement preparedStatement1 = connection.prepareStatement(sql1);
                preparedStatement1.setInt(1,1);
                money = money - 300;
                preparedStatement1.setInt(2,money);
                preparedStatement1.setString(3,account);
                preparedStatement1.executeUpdate();
                return 1;
            }
        }
        return -1;
    }

    @Override
    public int selectVip(Connection connection, String account) throws SQLException {
        ResultSet resultSet = null;
        String sql = "select * from user where student_id = ?";
        PreparedStatement preparedStatement =null;
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,account);
        resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            int member = resultSet.getInt("student_member");
            return member;
        }
        return -1;
    }

    @Override
    public boolean changePassword(Connection connection, String account, String oldPassword, String password) throws SQLException {
        ResultSet resultSet = null;
        String sql = "select * from user where student_id = ?";
        PreparedStatement preparedStatement =null;
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,account);
        resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            String student_password = resultSet.getString("student_password");
            if (student_password.equals(oldPassword)){
                String sql1 = "update user set student_password = ? where student_id = ?";
                PreparedStatement preparedStatement1 = null;
                preparedStatement1 = connection.prepareStatement(sql1);
                preparedStatement1.setString(1,password);
                preparedStatement1.setString(2,account);
                preparedStatement1.executeUpdate();
                return true;
            }else {
                return false;
            }
        }
        return false;
    }

    @Override
    public boolean forgetPassword(Connection connection, String account, String number, String code) throws SQLException {
        ResultSet resultSet = null;
        String sql = "select * from user where student_id = ?";
        PreparedStatement preparedStatement =null;
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,account);
        resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            String student_number = resultSet.getString("student_number");
            if (student_number.equals(number)){
                String sql1 = "update user set student_code = ? where student_id = ?";
                PreparedStatement preparedStatement1 = null;
                preparedStatement1 = connection.prepareStatement(sql1);
                preparedStatement1.setString(1,code);
                preparedStatement1.setString(2,account);
                int i = preparedStatement1.executeUpdate();
                if (i==1){
                    return true;
                }else {
                    return false;
                }
            }else {
                return false;
            }
        }else {
            return false;
        }
    }

    @Override
    public boolean changePassword(Connection connection, String account, String newPassword) throws SQLException {
        String sql = "update user set student_password = ? where student_id = ?";
        PreparedStatement preparedStatement = null;
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,newPassword);
        preparedStatement.setString(2,account);
        int i = preparedStatement.executeUpdate();
        if (i==1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean selectUser(Connection connection, String account) throws SQLException {
        String sql = "select * from user where student_id = ?";
        PreparedStatement preparedStatement =null;
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,account);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            return true;
        }
        return false;
    }
}

